﻿namespace OpenComposite.EII.Forms
{
	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Diagnostics;
	using System.Drawing;
	using System.IO;
	using System.Linq;
	using System.Windows.Forms;

	using Aga.Controls.Tree;

	using Microsoft.TeamFoundation;
	using Microsoft.TeamFoundation.Client;
	using Microsoft.TeamFoundation.VersionControl.Client;
	using OpenComposite.Base.Collections;
	using OpenComposite.Repository.Properties;
	using OpenComposite.Repository.Forms;
	using OpenComposite.Repository.TFS;

	public partial class OpenFileDialogEx : Form
	{
		#region Initialization

		public OpenFileDialogEx()
		{
			InitializeComponent();
			this.DialogResult = DialogResult.None;
			this.Multiselect = false;

			if ( Global.Status.IsTfsInstalled ) {
				_tfsexplorer = UCTfsExplorerFactory.Create(tabTfs, this);
			}
		}

		#endregion Initialization

		#region Public Members

		#region Properties

		/// <summary>
		/// Gets or sets a value indicating whether to allow Team Foundation Server support, will always return false if Team Explorer is not installed.
		/// </summary>
		/// <value><c>true</c> if TFS Support is allowed; otherwise, <c>false</c>.</value>
		public bool AllowTfsSupport
		{
			get { return Global.Status.IsTfsInstalled && _allowTfsSupport; }
			set { _allowTfsSupport = value && Global.Status.IsTfsInstalled; }
		}

		public bool AutoUpgradeEnabled
		{
			get;
			set;
		}

		public bool CheckFileExists
		{
			get;
			set;
		}

		public bool CheckPathExists
		{
			get;
			set;
		}

		public string DefaultExt
		{
			get;
			set;
		}

		public bool DisableFileSystemSupport
		{
			get;
			set;
		}

		public string FileName
		{
			get
			{
				if ( tabCtrlOpenType.SelectedTab == tabFileSystem ) {
					return _fileName;
				} else {
					return _tfsexplorer != null ? _tfsexplorer.FileName : null;
				}
			}
			set
			{
				_fileName = value;
				if ( _tfsexplorer != null ) _tfsexplorer.FileName = value;
			}
		}

		public string Filter
		{
			get;
			set;
		}

		public bool Multiselect
		{
			get;
			set;
		}

		public bool OpenFolder
		{
			get;
			set;
		}

		public string TfsUri
		{
			get { return _tfsexplorer != null ? _tfsexplorer.TfsUri : null; }
			set { if ( _tfsexplorer != null ) _tfsexplorer.TfsUri = value; }
		}

		public bool UseTfs
		{
			get { return tabCtrlOpenType.SelectedTab == tabTfs; }
		}

		public string Workspace
		{
			get { return _tfsexplorer != null ? _tfsexplorer.WorkspaceName : null; }
			set { if ( _tfsexplorer != null ) _tfsexplorer.WorkspaceName = value; }
		}

		#endregion Properties

		#region Methods

		#region Show methods (overridden)
		//Override Show() methods, OpenFileDialogEx should always be shown as modal dialog.
		[Obsolete("The method Show() is not available, use ShowDialog() instead.", true)]
		public new void Show()
		{
			throw new InvalidOperationException("OpenFileDialogEx can only be opened in modal dialog.");
		}

		[Obsolete("The method Show(IWin32Window) is not available, use ShowDialog(IWin32Window) instead.", true)]
		public new void Show(IWin32Window owner)
		{
			throw new InvalidOperationException("OpenFileDialogEx can only be opened in modal dialog.");
		}
		#endregion

		#endregion Methods

		#endregion Public Members

		#region Private Members

		#region Fields

		private bool _allowTfsSupport = Global.Status.IsTfsInstalled;

		private ITfsExplorer _tfsexplorer = null;
		private string _fileName;

		#endregion Fields

		#region Event Handlers

		private void OpenFileDialogEx_Shown(object sender, EventArgs e)
		{
			lblFileTitle.Text = string.Format(lblFileTitle.Text,
				OpenFolder ? "folder" : "file");
			if ( DisableFileSystemSupport ) {
				tabCtrlOpenType.TabPages.Remove(tabFileSystem);
			}
			if ( AllowTfsSupport ) {
				if ( _tfsexplorer.Init() ) {
					tabCtrlOpenType.SelectedTab = tabTfs;
				}
			} else {
				tabCtrlOpenType.TabPages.Remove(tabTfs);
			}
			if ( tabCtrlOpenType.TabPages.Count == 0 ) {
				throw new InvalidOperationException("At least one option for OpenFileDialogEx needs to be available");
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		private void btnOpenFile_Click(object sender, EventArgs e)
		{
			if ( OpenFolder ) {
				Vista_Api.FolderBrowserDialog dlg = new Vista_Api.FolderBrowserDialog();
				dlg.SelectedPath = this.FileName;
				if ( dlg.ShowDialog() == DialogResult.OK ) {
					this.FileName = dlg.SelectedPath;
					this.DialogResult = DialogResult.OK;
					this.Close();
				}
			} else {
				OpenFileDialog dlg = new OpenFileDialog();
				dlg.DefaultExt = this.DefaultExt;
				dlg.AutoUpgradeEnabled = this.AutoUpgradeEnabled;
				dlg.CheckFileExists = this.CheckFileExists;
				dlg.CheckPathExists = this.CheckPathExists;
				dlg.Filter = this.Filter;
				dlg.InitialDirectory = this.FileName;
				dlg.Multiselect = this.Multiselect;
				if ( dlg.ShowDialog() == DialogResult.OK ) {
					this.FileName = dlg.FileName;
					this.DialogResult = DialogResult.OK;
					this.Close();
				}
			}
		}

		#endregion Event Handlers

		#region Methods

		#endregion Methods

		#endregion Private Members

	}
}